package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Util;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class exodus implements Listener {
    @EventHandler
    public void on(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) {
            if (event.getDamager().getType().equals(EntityType.ARROW)) {
                Arrow arrow = (Arrow) event.getDamager();
                if (arrow.getShooter() instanceof Player && event.getEntity() instanceof Player) {
                    Player player = (Player) arrow.getShooter();
                    ItemMeta itemMeta = player.getInventory().getHelmet().getItemMeta();
                    if (itemMeta == null) return;
                    if (itemMeta.getDisplayName().equals(Util.getStr("Exodus"))) {
                        if (player.hasPotionEffect(PotionEffectType.REGENERATION)) return;
                        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 50, 1), true);
                    }
                }
            }
            return;
        }

        //对非玩家实体造成伤害则返回
        if (!(event.getEntity() instanceof Player)) return;
        Player player = (Player) event.getDamager();
        if (player.getInventory().getHelmet() == null) return;
        ItemMeta itemMeta = player.getInventory().getHelmet().getItemMeta();
        if (itemMeta == null) return;
        if (itemMeta.getDisplayName().equals(Util.getStr("Exodus"))) {
            if (player.hasPotionEffect(PotionEffectType.REGENERATION)) return;
            player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 50, 1), true);
        }
    }
}
